package com.itheima.work;

import com.itheima.util.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

// work模式,消费者1
public class Consumer1 {
    public static void main(String[] args) throws Exception {
        // 创建连接
        Connection connection = ConnectionUtil.getConnection();
        // 创建消息通道
        Channel channel = connection.createChannel();
        // 创建队列
        channel.queueDeclare(Producer.QUEUE_NAME,true,false,false,null);
        // 创建队列之后,如果监听到有数据,就会自动创建一个默认的消费者,接收消息
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                // 路由key
                System.out.println("路由key是:"+envelope.getRoutingKey());
                // 交换机
                System.out.println("交换机是:"+envelope.getExchange());
                // 消息的id
                System.out.println("消息的id是:"+envelope.getDeliveryTag());
                // 接收到消息
                System.out.println("接收到的消息是:"+new String(body,"utf-8"));
            }
        };
        // 继续监听队列
        channel.basicConsume(Producer.QUEUE_NAME,true,defaultConsumer);

    }
}
